package com.mt.common.response;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mt.common.enums.AppReturnCodeEnum;

import java.util.List;

/**
 * @Author: plover
 * @Date: 2020/07/06/4:25 下午
 * @Description:
 */
public class WebResponseUtils {

    public static <T> WebResponse<T> success() {
        return WebResponse.<T>builder()
                .code(AppReturnCodeEnum.SUCCESS.getCode())
                .message(AppReturnCodeEnum.SUCCESS.getMsg())
                .build();
    }


    public static <T> WebResponse<T> success(T t) {
        return WebResponse.<T>builder()
                .code(AppReturnCodeEnum.SUCCESS.getCode())
                .message(AppReturnCodeEnum.SUCCESS.getMsg())
                .data(t)
                .build();
    }

    public static <T> WebResponse<JSONObject> successJSONObject(String key, T t) {
        JSONObject result = new JSONObject();
        result.put(key, t);
        return WebResponse.<JSONObject>builder()
                .code(AppReturnCodeEnum.SUCCESS.getCode())
                .message(AppReturnCodeEnum.SUCCESS.getMsg())
                .data(result)
                .build();
    }

    public static <T> WebResponse<JSONObject> successList(List t) {
        JSONObject result = new JSONObject();
        result.put("list", t);
        result.put("total", t.size());
        return WebResponse.<JSONObject>builder()
                .code(AppReturnCodeEnum.SUCCESS.getCode())
                .message(AppReturnCodeEnum.SUCCESS.getMsg())
                .data(result)
                .build();
    }

    public static <T> WebListResponse<T> successListV2(List<T> t) {
        DataListDetail dataListDetail = new DataListDetail(t, t.size());
        return WebListResponse.<T>builder()
                .code(AppReturnCodeEnum.SUCCESS.getCode())
                .message(AppReturnCodeEnum.SUCCESS.getMsg())
                .data(dataListDetail)
                .build();
    }


    public static <T> WebResponse<JSONObject> successPage(Page t) {
        JSONObject result = new JSONObject();
        result.put("list", t.getRecords());
        JSONObject pageInfo = new JSONObject();
        pageInfo.put("total", t.getTotal());
        pageInfo.put("pages", t.getPages());
        pageInfo.put("pageSize", t.getSize());
        pageInfo.put("pageNum", t.getCurrent());
        result.put("pageInfo", pageInfo);
        return WebResponse.<JSONObject>builder()
                .code(AppReturnCodeEnum.SUCCESS.getCode())
                .message(AppReturnCodeEnum.SUCCESS.getMsg())
                .data(result)
                .build();
    }

    public static <T> WebPageResponse<T> successPageV2(Page<T> page) {
        PageInfo pageInfo = PageInfo.builder().pages(page.getPages())
                .total(page.getTotal()).pageNum(page.getCurrent()).pageSize(page.getSize()).build();
        PageDetail pageDetail = new PageDetail(page.getRecords(), pageInfo);
        return WebPageResponse.builder().code(AppReturnCodeEnum.SUCCESS.getCode())
                .message(AppReturnCodeEnum.SUCCESS.getMsg())
                .data(pageDetail).build();
    }

    public static <T> WebResponse<T> error(String message) {
        return WebResponse.<T>builder()
                .code(AppReturnCodeEnum.ERROR.getCode())
                .message(message)
                .build();
    }

    public static <T> WebResponse<T> error(Integer code, String message) {
        return WebResponse.<T>builder()
                .code(code)
                .message(message)
                .build();
    }

    public static <T> WebResponse<T> errorParam(String message) {
        return WebResponse.<T>builder()
                .code(AppReturnCodeEnum.PARAM_ERROR.getCode())
                .message(message)
                .build();
    }

    public static <T> WebListResponse<T> errorListParam(String message) {
        return WebListResponse.<T>builder()
                .code(AppReturnCodeEnum.PARAM_ERROR.getCode())
                .message(message)
                .build();
    }

    public static <T> WebPageResponse<T> errorPageParam(String message) {
        return WebPageResponse.<T>builder()
                .code(AppReturnCodeEnum.PARAM_ERROR.getCode())
                .message(message)
                .build();
    }

    public static <T> WebResponse<T> error(AppReturnCodeEnum returnCodeEnum) {
        return WebResponse.<T>builder()
                .code(returnCodeEnum.getCode())
                .message(returnCodeEnum.getMsg())
                .build();
    }


    public static <T> WebResponse<T> unAuthError() {
        return WebResponse.<T>builder()
                .code(AppReturnCodeEnum.UN_AUTH.getCode())
                .message(AppReturnCodeEnum.UN_AUTH.getMsg())
                .build();
    }

}
